package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"time"

	"./Duoble_link"
)

func main333() {

	dlink := DuobleLink.NewDoubleLinkList()
	dnode1 := DuobleLink.NewDoubleLinkNode("a1")
	dnode2 := DuobleLink.NewDoubleLinkNode("a2")

	dnode3 := DuobleLink.NewDoubleLinkNode("a3")
	dnode4 := DuobleLink.NewDoubleLinkNode("a4")
	dlink.InsertHead(dnode1)
	dlink.InsertHead(dnode2)
	//dlink.InsertHead(dnode3)

	//dlink.InsertHead(dnode4)

	dlink.InsertValueHead(dnode2, dnode3)
	dlink.InsertValueBack(dnode2, dnode4)

	//dnode5 := DuobleLink.NewDoubleLinkNode("a5")
	//dlink.InsertBack(dnode5)

	//fmt.Println(dlink.GetNodeAtindex(0))
	//fmt.Println(dlink.GetNodeAtindex(1))
	//fmt.Println(dlink.GetNodeAtindex(2))
	//fmt.Println(dlink.GetNodeAtindex(3))

	fmt.Println(dlink.String())

	dlink.DeleteNode(dnode2)

	fmt.Println(dlink.String())
}

//
func main() {
	dlink := DuobleLink.NewDoubleLinkList()

	t1 := time.Now() // 开始时间

	path1 := "D:\\golang\\day4\\猴岛游戏社区\\houdao\\1_1.txt"
	path2 := "D:\\golang\\day4\\猴岛游戏社区\\houdao\\1_2.txt"
	path3 := "D:\\golang\\day4\\猴岛游戏社区\\houdao\\1_3.txt"

	readFile(path1, dlink)
	readFile(path2, dlink)
	readFile(path3, dlink)

	fmt.Println("读取时间", time.Since(t1))

	for {
		fmt.Println("请输入要查询的数据")
		var inputstr string
		fmt.Scanln(&inputstr)

		starttime := time.Now() //时间开始

		dlink.FindString(inputstr)

		fmt.Println("一共用了", time.Since(starttime))
	}
}

func readFile(path string, dlist *DuobleLink.DuobleLinkList) {

	qqfile, _ := os.Open(path) //打开文件
	defer qqfile.Close()       // 延迟关闭

	bf := bufio.NewReader(qqfile) // 读取数据
	i := 0

	for {
		line, _, err := bf.ReadLine() //读取一样文件

		if err == io.EOF { //读取到文件的最后一样
			break
		}

		linestr := string(line) //转化成字符串

		node := DuobleLink.NewDoubleLinkNode(linestr)
		dlist.InsertHead(node)
		i++

	}

	fmt.Println("读取数据条数:", i)

}
